package com.situ.Servlet.ServletContest;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.student.entity.Student;
import com.situ.student.service.IStudentService;
import com.situ.student.service.impl.StudentServiceImpl;

public class ServletContest_1 extends HttpServlet{
	/**
	 * ServletContest域，练习
	 */
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");//post乱码
		resp.setContentType("text/html; charset=utf-8");//服务器响应浏览器乱码问题
		ServletContext servletContext = getServletContext();//相当创建servleContest
		
		/**
		 * 打印人名与学生列表
		 */
		servletContext.setAttribute("name", "马柳");//填入数据,在contest2中调出
		IStudentService service = new StudentServiceImpl();//准备调学生列表
		List<Student> list = service.list();
		servletContext.setAttribute("list", list);//放入学生列表
		
		/**
		 * 访问页面人数+1，借此了解ServleContext域
		 * 
		 * InitSrevlet全局初始化后，不用if了
		 */
		Integer count = (Integer) servletContext.getAttribute("count");//拿出来count
//		if (count == null) {
//			count = 1;
//		}else {
			count = count + 1;
//		}
		servletContext.setAttribute("count", count);//设置完，放进去
		PrintWriter writer = resp.getWriter(); //不经过jsp页面直接浏览器打印
		writer.println("访客： " + count);
		writer.close();
	}
}
